package com.ych.springmvc.controller;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author yangchenghao
 */
@Controller
public class ProtalController {
//    @RequestMapping("/")
//    public String protal() {
//        //将逻辑视图返回
//        return "index";
//    }

    @RequestMapping("/paramtest")
    public String paramTest(@RequestParam(value = "userName", required = true, defaultValue = "ccc") String username, String password) {
        System.out.println("username:" + username);
        System.out.println("password:" + password);
        return "success";
    }

    @RequestMapping(value = "/hello/{username}/{id}", method = {RequestMethod.GET, RequestMethod.POST})
    public String hello(@PathVariable("id") Integer id, @PathVariable("username") String usernmae) {
        System.out.println("username:" + usernmae);
        System.out.println("id:" + id);
        return "success";
    }

    @RequestMapping("/mav")
    public ModelAndView testModeAndView() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("testRequestScope", "helloMAV");
        mav.setViewName("success");
        return mav;
    }

    @RequestMapping("/model")
    public String testModel(Model model) {
        System.out.println(model.getClass().getName());
        model.addAttribute("testRequestScope", "helloModel");
        return "success";
    }

    @RequestMapping("/modelmap")
    public String testModelMap(ModelMap modelMap) {
        System.out.println(modelMap.getClass().getName());
        modelMap.addAttribute("testRequestScope", "helloModelMap");
        return "success";
    }

    @RequestMapping("/map")
    public String testModelMap(Map<String, Object> map) {
        System.out.println(map.getClass().getName());
        map.put("testRequestScope", "helloMap");
        return "success";
    }

    @RequestMapping("/session")
    public String testSession(HttpSession session) {
        session.setAttribute("yyy", "helloSession");
        return "success";
    }

    @RequestMapping("/application")
    public String testApplication(HttpSession session) {
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("yyy", "helloApplication");
        return "success";
    }
}
